﻿/***********************************************************************
 *            Project: boda.utilities                                  
 *                Web:                            
 *        ProjectName: 通用类                                
 *             Author: 杨云军                                          
 *              Email: yyj_17@163.com                                
 *           Versions: 1.0                                             
 *         CreateTime: 2020-02-01 17:48:52
 *          NameSpace: boda.utilities.Extensions
 *           FileName: boda.utilities
 *   ClassDescription:
 ***********************************************************************/
using System.Text;

using Newtonsoft.Json;

namespace boda.utilities.Extensions
{
    public static class JsonExtensions
    {
        public static string ObjToJson(this object value)
        {
            return JsonConvert.SerializeObject(value, Formatting.Indented);
        }

        public static T? JsonToObject<T>(this string json) where T : class
        {
            return JsonConvert.DeserializeObject<T>(json);
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static byte[] Serialize(this object item)
        {
            var jsonString = JsonConvert.SerializeObject(item);

            return Encoding.UTF8.GetBytes(jsonString);
        }
    }
}
